using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TfsFieldType = Microsoft.TeamFoundation.WorkItemTracking.Client.FieldType;

namespace TXLooker.WorkItemExtention.WIQL
{
    public class WorkItemFieldDefinition : ICloneable
    {
        private static Dictionary<TfsFieldType, OlUserPropertyType> tfsFieldType2OlFieldType = new Dictionary<TfsFieldType, OlUserPropertyType>();

        static WorkItemFieldDefinition()
        {
            tfsFieldType2OlFieldType[TfsFieldType.DateTime] = OlUserPropertyType.olDateTime;
            tfsFieldType2OlFieldType[TfsFieldType.Double] = OlUserPropertyType.olNumber;
            tfsFieldType2OlFieldType[TfsFieldType.Integer] = OlUserPropertyType.olInteger;
        }

        public WorkItemFieldDefinition()
        {
            HelpText = string.Empty;
            FieldType = FieldType.Html;
            Name = string.Empty;
            ReferenceName = string.Empty;
            ID = -1;
        }

        public WorkItemFieldDefinition(FieldDefinition field)
            : this()
        {
            this.Init(field);
        }

        public WorkItemFieldDefinition(WorkItemFieldDefinition field)
            : this()
        {
            this.Init(field);
        }

        public void Init(FieldDefinition field)
        {
            this.ID = field.Id;
            this.ReferenceName = field.ReferenceName;
            this.Name = field.Name;
            this.CanSortBy = field.CanSortBy;
            this.FieldType = field.FieldType;
            this.HelpText = field.HelpText;
            this.IsEditable = field.IsEditable;
            this.IsQueryable = field.IsQueryable;
            this.SystemType = field.SystemType;
        }

        public void Init(WorkItemFieldDefinition field)
        {
            this.ID = field.ID;
            this.ReferenceName = field.ReferenceName;
            this.Name = field.Name;
            this.CanSortBy = field.CanSortBy;
            this.FieldType = field.FieldType;
            this.HelpText = field.HelpText;
            this.IsEditable = field.IsEditable;
            this.IsQueryable = field.IsQueryable;
            this.SystemType = field.SystemType;
        }

        public FieldDefinition GetFieldDefinition(WorkItemStore store)
        {
            return store.FieldDefinitions.GetById(this.ID);
        }

        public int ID { get; protected set; }

        public string ReferenceName { get; protected set; }

        public string Name { get; protected set; }

        public bool CanSortBy { get; protected set; }

        public FieldType FieldType { get; protected set; }

        /// <summary>
        /// Gets the type of the ol field. <see cref="OlUserPropertyType.olFormula"/> by default.
        /// </summary>
        /// <value>The type of the ol field.</value>
        public OlUserPropertyType OlFieldType
        {
            get
            {
                return tfsFieldType2OlFieldType.ContainsKey(FieldType)
                               ? tfsFieldType2OlFieldType[FieldType]
                               : OlUserPropertyType.olFormula;
            }
        }

        public string HelpText { get; protected set; }

        public bool IsEditable { get; protected set; }

        public bool IsQueryable { get; protected set; }

        public Type SystemType { get; protected set; }

        #region ICloneable Members

        public object Clone()
        {
            WorkItemFieldDefinition clone = new WorkItemFieldDefinition();
            clone.Init(this);
            return clone;
        }

        #endregion

        public static implicit operator WorkItemFieldDefinition(FieldDefinition wiFieldDef)
        {
            return new WorkItemFieldDefinition(wiFieldDef);
        }
    }
}
